var timer_my_request;
var latlng = new google.maps.LatLng(31.2292969,121.459026);
var myOptions = {
	zoom: 14,
	center: latlng,
	streetViewControl: false,
	panControl: false,
	zoomControl: false,
	mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map;
var miniMap;
var markersArray = [];
var overlay = null;

//附件场所查找条件
var currentCenter = "31.2292969,121.459026";
var selectedPlaceType = "";

$(document).ready(function(){
	$("#map_canvas").width($(document).width() - 350);
	$(window).resize(function(){
		if ($("#div_maintain_request_detail").css("display") == "none") {
			$("#map_canvas").width($(document).width() - 350);
		} else {
			$("#map_canvas").width($(document).width() - 350 - $("#div_maintain_request_detail").width() - 10);
		}
	});
	
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	google.maps.event.addListener(map, 'dragend', function() {
		if (overlay != null) {
			overlay.draw();
		}
		
		currentCenter = map.getCenter().lat() + "," + map.getCenter().lng();
		//freshPlaces();
	});
	
	var releaseControlDiv = document.createElement('DIV');
    var releaseControl = new ReleaseControl(releaseControlDiv, map);

    releaseControlDiv.index = 1;
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(releaseControlDiv);
	
	freshPlaces();
});

function showRequestDetail(requestId) {
	try {
		requestUrl = "customer_json_servlet";
		var params = {
			request_method:"get_request_detail",
			request_id:requestId
		};
		
		$.post(requestUrl, params, function(data){
			try {
				var requestDetail = data.resultData;
				var type = (requestDetail.type == 1 ) ? "<求帮助>" : "<求养护>";
				
				$("#div_maintain_request_detail").html("");
				$("#div_maintain_request_detail").css("display", "");
				var innerHtml = "";
				clearInterval(timer_my_request);
		
				innerHtml += '	<div style="width: 390px; height: 50px; font-size: 15pt; font-weight: bold; border-bottom: 1px solid #c8c8c8;">';
				innerHtml += '		<span style="padding-top: 14px;">需求信息' + type + '</span>';
				innerHtml += '	</div>';
				if (requestDetail.remainTime > 0) {
					innerHtml += '	<div style="padding-top: 15px; font-size: 14pt; color: red;">';
					innerHtml += '		<span style="width: 115px; display: inline-block;">协助倒计时:</span>';
					innerHtml += '		<span id="remain_time"></span>';
					innerHtml += '	</div>';
				}
				innerHtml += '	<div style="padding-top: 15px; font-size: 11pt;">';
				innerHtml += '		<span style="width: 115px; display: inline-block;">车辆信息:</span>';
				innerHtml += '		<span>' + requestDetail.carModel + '</span>';
				innerHtml += '	</div>';
				innerHtml += '	<div style="padding-top: 15px; font-size: 11pt;">';
				innerHtml += '		<span style="width: 115px; display: inline-block;">当前位置:</span>';
				innerHtml += '		<span>' + requestDetail.address + '</span>';
				innerHtml += '	</div>';
				innerHtml += '	<div style="padding-top: 15px; font-size: 11pt;">';
				innerHtml += '		<span style="width: 115px; display: inline-block;">联系方式:</span>';
				innerHtml += '		<span>' + requestDetail.customerMobile + '</span>';
				innerHtml += '	</div>';
				innerHtml += '	<div style="padding-top: 15px; font-size: 11pt;">';
				innerHtml += '		<span style="width: 115px; display: inline-block;">求助内容:</span><br/>';
				innerHtml += '		<textarea style="width: 379px; height: 113px; border: none; background: url(\'../image/bg_info_textarea.png\') no-repeat;" disabled="disabled">' + requestDetail.content + '</textarea><br/>';
				innerHtml += '	</div>';
				innerHtml += '	<div style="width: 390px; height: 10px; font-size: 15pt; font-weight: bold; border-bottom: 1px solid #c8c8c8;"></div>';
				innerHtml += '	<div style="padding-top: 15px; font-size: 14pt;">';
				innerHtml += '		<span style="width: 115px; display: inline-block;">留言:</span><br/>';
				innerHtml += '	</div>';
				innerHtml += '	<div id="request_messages" align="left"></div>';
				innerHtml += '	<div style="padding-top: 15px; font-size: 11pt;">';
				innerHtml += '		<textarea id="request_message" style="width: 379px; height: 113px; border: none; background: url(\'../image/bg_info_textarea.png\') no-repeat;"></textarea><br/>';
				innerHtml += '	</div>';
				innerHtml += '	<div style="padding-top: 15px;">';
				innerHtml += '		<img class="pointer fade_effect" src="../image/btn_add_message.png" onclick="addRequestMessage(\'' + requestDetail.id + '\');"/>';
				innerHtml += '	</div>';
				
				$("#div_maintain_request_detail").html(innerHtml);
				showRequestMessages(requestDetail.id);
				
				timer_my_request = setInterval(function(){
					if (eval(requestDetail.remainTime) <= 0) {
						$("#remain_time").html("已过期");
					} else {
						requestDetail.remainTime -= 1000;
						$("#remain_time").html(getTimeStr(eval(requestDetail.remainTime)) + "<进行中>");
					}
				}, 1000);
		
				addFadeEffect();
				
				$("#map_canvas").width($(document).width() - 350 - $("#div_maintain_request_detail").width() - 10);
			} catch (e) {
				showTestMessage(e);
			}
		}, "json");
	} catch (e) {
		showTestMessage(e);
	}
}

function showRequestMessages(requestId) {
	try {
		requestUrl = "customer_json_servlet";
		var params = {
			request_method:"get_request_messages",
			request_id:requestId
		};
		
		$.post(requestUrl, params, function(data) {
			try {
				var requestMessages = data.resultData.items;
				var innerHtml = "";
				
				for(var i = 0; requestMessages != null && i < requestMessages.length; i++) {
					innerHtml += '	<div style="margin-top: 15px; height: 27px; width: 380px; font-size: 11pt; background: url(../image/bg_comment_title.png);">';
					innerHtml += '		<span style="width: 115px; float: left;">' + requestMessages[i].userId + '</span>';
					innerHtml += '		<span style="width: 185px; float: right;">' +  requestMessages[i].add_time + '</span>';
					innerHtml += '	</div>';
					innerHtml += '	<div style="padding-top: 2px; font-size: 11pt;" align="left">';
					innerHtml += '		<span>' + requestMessages[i].message + '</span>';
					innerHtml += '	</div>';
				}
				
				$("#request_messages").html(innerHtml);
			} catch (e) {
				showTestMessage(e);
			}
			
		}, "json");
		
	} catch (e) {
		showTestMessage(e);
	}
}

function addRequestMessage(requestId) {
	try {
		requestUrl = "customer_json_servlet";
		var params = {
			request_method:"add_request_message",
			request_id:requestId,
			request_message:$("#request_message").val()
		};
		
		$.post(requestUrl, params, function(data) {
			if (data.code == 0) {
				showRequestMessages(requestId);
			}
		}, "json");
	} catch (e) {
		showTestMessage(e);
	}
}

function toggleTypeList() {
	if ($("#slt_type_list").css("display") == "none") {
		$("#slt_type_list").css("left", $("#slt_type").offset().left + 2);
		$("#slt_type_list").css("top", $("#slt_type").offset().top + 42);
		$("#slt_type_list").css("display", "");
	} else {
		$("#slt_type_list").css("display", "none");
	}
	
	$("#slt_district_list").css("display", "none");
	$("#slt_place_type_list").css("display", "none");
}

function toggleDistrictList() {
	if ($("#slt_district_list").css("display") == "none") {
		$("#slt_district_list").css("left", $("#slt_district").offset().left + 2);
		$("#slt_district_list").css("top", $("#slt_district").offset().top + 42);
		$("#slt_district_list").css("display", "");
	} else {
		$("#slt_district_list").css("display", "none");
	}
	
	$("#slt_type_list").css("display", "none");
	$("#slt_place_type_list").css("display", "none");
}

function ReleaseControl(controlDiv, map) {
	controlDiv.style.margin = "20px";
	
	// Set CSS for the control border
	var controlUI = document.createElement('DIV');
	controlUI.style.background = 'url("../image/btn_release_request.png")';
	controlUI.style.width = "215px";
	controlUI.style.height = "43px";
	controlUI.style.cursor = 'pointer';
	controlUI.style.textAlign = 'center';
	controlDiv.appendChild(controlUI);
  
	google.maps.event.addDomListener(controlDiv, 'click', function() {
		$("#dlg_my_request").css("left", $(controlDiv).offset().left - 20);
		$("#dlg_my_request").css("top", $(controlDiv).offset().top + 30);
		$("#dlg_my_request").css("display", "");
		
		miniMap = new google.maps.Map(document.getElementById("mini_map"), myOptions);
	});
}

function pinToAddress() {
	requestUrl = "../get_gps_info";
	var params = {
		address:$("#address").val()
	};
	
	$.post(requestUrl, params, function(data) {
		if (data.code == 0) {
			alert(data.resultData);
			
			miniMap.setCenter(new google.maps.LatLng(31.2292969,121.459026));
		}
	}, "json");
}